<template>
	<view class="list">
		<view v-if="info.list.length">
			<u-swipe-action btn-width="240" v-for="(item,index) in info.list" :key="index" :options="info.options"
				@click="(i: any,index2:number,text:string)=>setting(item,index2)">
				 <!-- @click.stop="choose(item)" -->
				<view class="flex flex-jc-sb flex-ai-c list-i">
					<view>
						<view class="font-32 font-bold">{{item.company}}</view>
						<view class="flex flex-ai-c font-26" style="margin: 22rpx 0 30rpx; color: #333333;">
							<view>{{item.position}}</view>
							<view v-if="item.is_default" style="background: #000;color: #fff;width: 80rpx;text-align: center;margin-left: 20rpx;">默认</view>
							<!-- is_default -->
						</view>
					</view>
					<view @click.stop="edit(item)">
						<u-icon name="edit-pen" size="30rpx" />
					</view>
				</view>
			</u-swipe-action>
		</view>
		<view v-else style="background-color: #fff;height: 100%;">
			<defaultImg />
		</view>

		<view class="btn flex flex-jc-c flex-ai-c" @click="edit()">
			<i-icon icon="icon-plus" size="24rpx" color="#fff" style="margin-right: 10rpx;" />
			添加企业
		</view>
		<view style="height: 248rpx;" v-if="info.list.length"></view>
	</view>

</template>

<script setup lang="ts">
	import defaultImg from '@/components/default-img.vue'
	import user from '../../api/user'
	import {
		reactive
	} from 'vue'
	import {
		onShow,onLoad,onUnload
	} from '@dcloudio/uni-app'
	let info = reactive({
		list: [],
		id: '',
		options: [
			{
				text: '设为默认',
				style: {
					backgroundColor: '#F5F6F7',
					color: '#333333'
				}
			},
			{
				text: '删除',
				style: {
					backgroundColor: '#000000',
					color: '#fff'
				}
			}
		]
	})
	//企业列表
	const companyGetList = async () => {
		info.list = []
		const res = await user.companyGetList()
		info.list = res.data
	}
	const opCompany = async (item) => {
		await user.opCompany(item)
		companyGetList()
	}
	const downCompany = async (item) => {
		await user.downCompany({
			id: item.id
		})
		companyGetList()
	}
	const choose = async (item: any) => {
		const res = await user.editMemberInfo({
			id: info.id,
			company_id: item.id
		})
		if(res.code === 200){
			let pages = getCurrentPages()
			let prevPage = pages[pages.length - 2]; //上一页页面实例
			prevPage.$vm.value = {
				text: item.company,
				type: 8
			}
			uni.navigateBack()
		}
		if(res.code === 0) {
			uni.navigateBack()
		}
		
	}
	const setting = (item : any, i : any) => {
		switch (i) {
			case 0:
				opCompany({
					...item,
					is_default: 1
				})
			break
			case 1:	//删除
				downCompany(item)
			break
		}
	}
	const edit = (item?: any) => {
		if(item) {	//编辑
			uni.navigateTo({
				url: `/pages/enterprise/add?type=edit&info=${JSON.stringify(item)}`
			})
		}else {	//新增
			uni.navigateTo({
				url: `/pages/enterprise/add?type=add`
			})
		}
	}
	onShow(() => {
		companyGetList()
	})
	onLoad((op) => {
		info.id = op.id
	})
	onUnload(() => {
		let pages = getCurrentPages()
		let prevPage = pages[pages.length - 2]; //上一页页面实例
		prevPage.$vm.value = {
			type: 8,
			obj: info.list.length ? info.list[0] : null
		}
	})
</script>
<style>
	page {
		height: 100%;
		background: #F8F8F8;
	}
</style>
<style lang="scss" scoped>
	.list {
		height: 100%;
		padding: 20rpx;
		.list-i{
			padding: 30rpx;
			border-bottom: 1rpx solid #EEEFF0;
		}
		.btn {
			position: fixed;
			width: calc(100% - 74rpx);
			left: 37rpx;
			height: 78rpx;
			background: #000000;
			border-radius: 39rpx;
			color: #fff;
			font-size: 32rpx;
			bottom: 100rpx;
		}
	}
</style>